package com.example.questionsystem.modules.qu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.questionsystem.modules.qu.entity.SubjectSessions;

import com.example.questionsystem.modules.qu.mapper.SubjectsMapper;
import com.example.questionsystem.modules.qu.service.SubjectsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SubjectsServiceImpl extends ServiceImpl<SubjectsMapper, SubjectSessions> implements SubjectsService {

    @Resource
    private SubjectsMapper subjectsMapper;

    @Override
    public void saveSubjectSessions(List<SubjectSessions> subjectSessionsList) {
        for (SubjectSessions subjectSessions : subjectSessionsList) {
            subjectsMapper.selectOne(new QueryWrapper<SubjectSessions>().eq("subject_id", subjectSessions.getSubjectId()));
            if (subjectSessions != null) {
               subjectsMapper.delete(new QueryWrapper<SubjectSessions>().eq("subject_id", subjectSessions.getSubjectId()));
            }
        }
       subjectsMapper.batchInsert(subjectSessionsList);
    }

    @Override
    public List<SubjectSessions> getSubjectSessionsByProjectId(String projectId) {
        QueryWrapper<SubjectSessions> SubjectSessionQueryWrapper = new QueryWrapper<>();
        SubjectSessionQueryWrapper.ge("project_id", projectId);
        return subjectsMapper.selectList(SubjectSessionQueryWrapper);
    }

    @Override
    public List<SubjectSessions> getSessionName(String projectId) {
        QueryWrapper<SubjectSessions> SubjectSessionQueryWrapper = new QueryWrapper<>();
        SubjectSessionQueryWrapper.ge("project_id", projectId);
        List<SubjectSessions> subjectSessionsList = subjectsMapper.selectList(SubjectSessionQueryWrapper);
        List<SubjectSessions> collect = subjectSessionsList.stream()
                .collect(Collectors.toMap(SubjectSessions::getSessionName, s -> s, (existing, replacement) -> existing))
                .values()
                .stream()
                .collect(Collectors.toList());
        return collect;
    }

    public List<SubjectSessions> getSessionName2() {
        QueryWrapper<SubjectSessions> SubjectSessionQueryWrapper = new QueryWrapper<>();
        List<SubjectSessions> subjectSessionsList = subjectsMapper.selectList(SubjectSessionQueryWrapper);
        List<SubjectSessions> collect = subjectSessionsList.stream()
                .collect(Collectors.toMap(SubjectSessions::getSessionName, s -> s, (existing, replacement) -> existing))
                .values()
                .stream()
                //kmxh 排序
                .sorted((o1, o2) -> o1.getKmxh().compareTo(o2.getKmxh()))
                .collect(Collectors.toList());
        return collect;
    }
    @Override
    public List<SubjectSessions> getSubjectSessions() {
        return subjectsMapper.selectList(null);
    }

    @Override
    public SubjectSessions getSubjectSessionsById(String subjectId) {
        QueryWrapper<SubjectSessions> subjectSessionsQueryWrapper = new QueryWrapper<>();
        return subjectsMapper.selectOne(subjectSessionsQueryWrapper.eq("subject_id", subjectId));
    }

    @Override
    public List<SubjectSessions> getSubjectBySessionName(String projectId, String sessionName) {
        QueryWrapper<SubjectSessions> subjectSessionsQueryWrapper = new QueryWrapper<>();
        subjectSessionsQueryWrapper.eq("project_id", projectId);
        subjectSessionsQueryWrapper.eq("session_name", sessionName);
        return subjectsMapper.selectList(subjectSessionsQueryWrapper);
    }

    @Override
    public List<SubjectSessions> getSubjects(String projectId, String subjectId) {
        QueryWrapper<SubjectSessions> subjectSessionsQueryWrapper = new QueryWrapper<>();
        subjectSessionsQueryWrapper.eq("project_id", projectId);
        subjectSessionsQueryWrapper.eq("subject_id", subjectId);
        return subjectsMapper.selectList(subjectSessionsQueryWrapper);
    }

    // 根据场次名称获取科目名称
    @Override
    public List<SubjectSessions> getSubjectsNameBySessionName(String sessionName) {
        QueryWrapper<SubjectSessions> subjectSessionsQueryWrapper = new QueryWrapper<>();
        subjectSessionsQueryWrapper.eq("session_name", sessionName);
        return subjectsMapper.selectList(subjectSessionsQueryWrapper);
    }

    @Override
    public List<SubjectSessions> getSubjectBySessionName(String sessionName) {
        QueryWrapper<SubjectSessions> subjectSessionsQueryWrapper = new QueryWrapper<>();
        subjectSessionsQueryWrapper.eq("session_name", sessionName);
        return subjectsMapper.selectList(subjectSessionsQueryWrapper);
    }

}